<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            // 先删除唯一索引
            $table->dropUnique(['email']);
        });
        
        Schema::table('users', function (Blueprint $table) {
            // 修改 email 字段为可空，然后重新添加唯一索引
            // MySQL 中唯一索引可以包含多个 NULL 值
            $table->string('email')->nullable()->change();
        });
        
        Schema::table('users', function (Blueprint $table) {
            // 重新添加唯一索引
            $table->unique('email');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropUnique(['email']);
        });
        
        Schema::table('users', function (Blueprint $table) {
            // 恢复 email 为必填
            $table->string('email')->nullable(false)->change();
        });
        
        Schema::table('users', function (Blueprint $table) {
            $table->unique('email');
        });
    }
};
